package com.jshoperxms.components.netease.sms;

import com.jshoperxms.components.netease.CheckSumBuilder;
import com.jshoperxms.components.netease.JPNeteaseKey;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sdywcd on 2018/7/10.
 * Des:通知消息类短信发送工具
 */
public class NeteaseSmsNoticeTool {

    //随机数
    private static final String NONCE="0011223342245566778899";


    /**
     * 发送预订陪练成功的短信
     * @return
     * @throws Exception
     */
    public static void sendBookingSparringNoticeToTeacher(String mobiles,String sendparams){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.BOOKINGSPARRINGTOTEACHERTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        nvps.add(new BasicNameValuePair("params", sendparams));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送预订成功消息给学生
     * @param mobiles
     * @param sendparams
     */
    public static void sendBookingSparringNoticeToStudent(String mobiles,String sendparams){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.BOOKINGSPARRINGTOSTUDENTTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        nvps.add(new BasicNameValuePair("params", sendparams));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送审核教师资格通知
     * @param mobiles
     */
    public static void sendVerifyTeacherQualificationsNotice(String mobiles){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.VRIFYTEACHERINGTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 学生试课申请后台通知
     * @param mobiles
     * @param sendparams
     */
    public static void sendStudentTryclassNotice(String mobiles,String sendparams){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.STUDENTTRYCLASSTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        nvps.add(new BasicNameValuePair("params", sendparams));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 试课前2小时短信提醒老师
     * @param mobiles
     * @param sendparams
     */
    public static void sendToTeacherTryclassBefore2hoursNotice(String mobiles,String sendparams){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.TRYCLASSNOTEICTOTEACHERTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        nvps.add(new BasicNameValuePair("params", sendparams));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 试课前2小时短信提醒学生
     * @param mobiles
     * @param sendparams
     */
    public static void sendToStudentTryclassBefore2hoursNotice(String mobiles,String sendparams){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.TRYCLASSNOTICETOSTUDENTTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        nvps.add(new BasicNameValuePair("params", sendparams));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 陪练前2小时短信提醒老师
     * @param mobiles
     * @param sendparams
     */
    public static void sendToTeacherSparringclassBefore2hoursNotice(String mobiles,String sendparams){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.SPARRINGNOTICETOTEACHERTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        nvps.add(new BasicNameValuePair("params", sendparams));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 陪练前2小时短信提醒学生
     * @param mobiles
     * @param sendparams
     */
    public static void sendToStudentSparringclassBefore2hoursNotice(String mobiles,String sendparams){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_NOTICE_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsNoticeTemplate.SPARRINGNOTICETOSTUDENTTEMPLATEDID));
        nvps.add(new BasicNameValuePair("mobiles", mobiles));
        nvps.add(new BasicNameValuePair("params", sendparams));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
